﻿using SuperX.Common.Log;
using SuperX.SuperSocket.Socket.Transfer.Client;
using SuperX.SuperSocket.Socket.Transfer.Server;

namespace SuperX.SuperSocket.Socket.Transfer.Client.UpLoad.Command
{
    internal class DoExists : UpLoadCommandBase
    {
        public override void ExecuteCommand(UpLoadClientEngine session, TransferCommandInfo commandInfo)
        {
            ServerUploadStateInfo info = SerializeHelp.Deserialize<ServerUploadStateInfo>(commandInfo.Data);

            if (info == null || info.CheckUploadType != CheckUploadType.Uploading)
            {
                if (info == null) Logger.Trace($"{nameof(ServerUploadStateInfo)} is null");

                bool result = session.OnFileExist();
                if (result)
                {
                    byte[] data = SerializeHelp.Serialize<FileInfo>(session.UpLoadInfo);
                    session.SendData(UpLoadOP.DoCover, data, 0, data.Length);
                }
                else
                {
                    session.DoEnd();
                }
            }
            else if (info?.CheckUploadType == CheckUploadType.Uploading)
            {
                session.OnExistUploading(info);
            }
        }

        public override string Name
        {
            get
            {
                return UpLoadOP.DoExists.ToString();
            }
        }
    }
}